import { Module } from '@nestjs/common';
import { SharedModule } from './shared/shared.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { UserModule } from './modules/user/user.module';
import { PermissionModule } from './modules/permission/permission.module';
import { RoleModule } from './modules/role/role.module';
import { ArticleModule } from './modules/article/article.module';
import { LogModule } from './modules/log/log.module';
import { WinstonModule } from 'nest-winston';
import * as winston from 'winston';
import 'winston-daily-rotate-file';
import { APP_FILTER, APP_GUARD, APP_INTERCEPTOR } from '@nestjs/core';
import SystemUnifyExceptionFilter from '@/common/filters/system-unify-exception.filter';
import { UserLoggingInterceptor } from '@/common/interceptors/userlogging.interceptor';
import { RankingModule } from './modules/ranking/ranking.module';
import { EmailModule } from './modules/email/email.module';
import { JwtModule } from '@nestjs/jwt';

import { ArticleCategoryModule } from '@/modules/article/article_category/category.module';
import { ArticleTagModule } from '@/modules/article/article_tag/tag.module';
import { LoginGuard } from '@/common/guards/login.guard';
import { SearchModule } from './modules/search/search.module';
import { CommentModule } from './modules/comment/comment.module';
import { SystemModule } from './modules/system/system.module';
import { AuthModule } from './modules/auth/auth.module';
import { PermissionsCacheService } from "@/shared/permissionsCache.service";
import { FileModule } from './modules/file/file.module';

@Module({
  imports: [
    /* 配置文件模块 */
    ConfigModule.forRoot({
      isGlobal: true,
      envFilePath: ['.env.local', '.env'],
    }),
    // jwt 模块
    JwtModule.registerAsync({
      global: true,
      useFactory(configService: ConfigService) {
        return {
          secret: configService.get('jwt_secret'),
          signOptions: {
            expiresIn: '30m',
          },
        };
      },
      inject: [ConfigService],
    }),
    WinstonModule.forRootAsync({
      imports: [ConfigModule],
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => ({
        transports: [
          new winston.transports.DailyRotateFile({
            dirname: configService.get<string>('LOG_DIRNAME') || 'logs',
            filename: configService.get<string>('LOG_FILENAME') || '%DATE%.log',
            datePattern: configService.get<string>('LOG_DATE_PATTERN') || 'YYYY-MM-DD',
            zippedArchive: configService.get<boolean>('LOG_ZIPPED_ARCHIVE') || true,
            maxSize: configService.get<string>('LOG_MAX_SIZE') || '20m',
            maxFiles: configService.get<string>('LOG_MAX_FILES') || '14d',
            format: winston.format.combine(
              winston.format.timestamp({
                format: 'YYYY-MM-DD HH:mm:ss',
              }),
              winston.format.json(),
            ),
          }),
        ],
      }),
    }),

    UserModule,
    PermissionModule,
    RoleModule,

    SharedModule,
    ArticleModule,
    ArticleCategoryModule,
    ArticleTagModule,
    LogModule,
    RankingModule,
    EmailModule,
    SearchModule,
    CommentModule,
    SystemModule,
    AuthModule,
    FileModule,
  ],
  providers: [
    // 注册全局异常过滤器
    {
      provide: APP_FILTER,
      useClass: SystemUnifyExceptionFilter,
    },
    {
      provide: APP_INTERCEPTOR,
      useClass: UserLoggingInterceptor,
    },
    {
      provide: APP_GUARD,
      useClass: LoginGuard,
    },
    PermissionsCacheService
  ],
})
export class AppModule {}
